package scrollable;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ScrollableTest {
	public static void main(String[] args) {
		try {
			// load driver
			// Class.forName("net.sourceforge.jtds.jdbc.Driver");
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			System.out.println("driver da duoc load ");
			// tao ket noi den CSDL
			// Connection connection = DriverManager.getConnection(
			// "jdbc:jtds:sqlserver://localhost:1433/test", "sa",
			// "sa");
			Connection connection = DriverManager
					.getConnection("jdbc:sqlserver://localhost:1433;databaseName=demo;user=sa;password=123456");
			System.out.println("Da ket noi duoc voi CSDL");
			// tao cau truy van
			String sql = "SELECT * FROM SinhVien";
			// thuc hien cau truy van
			Statement state = connection
					.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
							ResultSet.CONCUR_UPDATABLE);

			ResultSet result = state.executeQuery(sql);
			System.out.println("before delete");
			while (result.next()) {
				System.out.println(result.getString(1) + "\t"
						+ result.getString(2) + "\t");
			}
			// update su dung scrollable
			// result.first();
			// result.updateString(2, "ccc");
			// result.updateRow();

			// insert row
			/*
			 * result.moveToInsertRow();
			 * 
			 * result.updateString(2, "Minh"); result.updateString("Email",
			 * "minh122@gmail.com"); result.insertRow();
			 */

			// update row with row index
			/*
			 * result.absolute(3); result.updateString("TenSV", "cao minh");
			 * result.updateRow();
			 */

			// delete row
			// result.absolute(2);
			// result.deleteRow();

			// result.beforeFirst();
			// System.out.println("after delete");
			while (result.next()) {
				System.out.println(result.getString(1) + "\t"
						+ result.getString(2) + "\t");
			}

		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
		} catch (SQLException ex) {
			ex.printStackTrace();
		}

	}
}
